/*
 * @author zei , QQ:77132995, email:kazeik@163.com
 * 2022-03-12 14:10
 * 类说明:
 */
import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/provider/dashboard/data_import/setting_dataimport_summary_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/common/tag_widget.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/input/search_input_field.dart';
import 'package:finance_app/src/widgets/list_item/chart_account_open_balance_item.dart';
import 'package:finance_app/src/widgets/list_item/time_select_item.dart';
import 'package:flutter/material.dart';

@RouterMeta(paramName: "settingDataImportSummary")
class SettingDataImportSummaryPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _SettingDataImportSummaryPage();
}

class _SettingDataImportSummaryPage extends State<SettingDataImportSummaryPage>
    with PagePresenter<SettingDataImportSummaryPage, SettingDataImportSummaryProvider> {
  @override
  Widget get buildWidget => ListView(
        shrinkWrap: true,
        children: [
          //日期栏
          AppSpaces.h20,
          listBuilder<DateTime>(
            selector: (_, __) => __.asAt,
            builder: (context, value, child) => TimeSelectItem(
                title: S.current.as_at,
                value: value == null ? Config.settingInfo.dateFormat.toUpperCase() ?? "DD MMM YYYY" : DateTimeUtils.formatGlobalDateTime(value),
                onTap: provider.onSelectAsAt),
          ),
          // 搜索栏
          Container(
            margin: EdgeInsets.only(
              top: getDp(20),
              right: getDp(10),
              left: getDp(10),
            ),
            child: SearchInputField(
              controller: provider.searchController,
              focusNode: provider.searchFocusNode,
            ),
          ),
          _infoItem(S.current.account, "${S.current.opening_balances}(SGD)", rbood: true, lbold: true),
          ListView.separated(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: provider.data == null ? 0 : provider.data.length,
            itemBuilder: (ctx, index) => Container(
              margin: EdgeInsets.only(
                right: getDp(10),
                left: getDp(10),
              ),
              child: ListTile(
                title: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(
                      flex: 3,
                      child: Text(
                        "${provider.data[index].name}",
                        style: TextStyle(
                          color: aceBlackColor,
                          fontSize: getSp(14),
                          fontWeight: FontWeight.w400,
                          height: 22 / 14,
                          fontFamily: "DinRoundPro",
                        ),
                      ),
                    ),
                    Container(
                      constraints: BoxConstraints(
                        maxWidth: getDp(120),
                      ),
                      child: PrefixInputField(
                        initValue: "${provider.data[index].default_balance}",
                        onUpdateValue: (str) => provider.updateBalance(str, index),
                      ),
                    )
                  ],
                ),
                subtitle: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(
                      "${provider.data[index].account_code}",
                      style: TextStyle(
                        color: aceBlackColor,
                        fontSize: getSp(14),
                        fontWeight: FontWeight.w400,
                        height: 22 / 14,
                        fontFamily: "DinRoundPro",
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(left: getDp(10), top: getDp(4)),
                      child: BlueTagWidget(
                        content: provider.data[index].crdr_nature == "DR" ? S.current.debit : S.current.credit,
                      ),
                    ),
                  ],
                ),
              ),
            ),
            separatorBuilder: (context, index) => Divider(),
          ),
          Divider(),
          _infoItem(S.current.total_debit, "${provider.totalDebit}", rbood: true),
          _infoItem(S.current.total_credit, "${provider.totalCredit}", rbood: true),
          _infoItem(S.current.difference, "${provider.difference}", rbood: true),
        ],
      );

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(title: S.current.data_import, actions: [
        NormalNextTextButton(onTap: () => provider.toNext() //provider.showConfirmImport(context),
            )
      ]);

  Widget _infoItem(String leftStr, String rightStr, {bool lbold = false, bool rbood = false}) => Container(
        margin: EdgeInsets.only(
          top: getDp(10),
          bottom: getDp(10),
        ),
        padding: EdgeInsets.only(
          left: getDp(30),
          right: getDp(30),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              leftStr ?? "",
              style: TextStyle(
                color: aceBlackColor,
                fontSize: getSp(16),
                fontWeight: lbold == true ? FontWeight.w700 : FontWeight.normal,
                height: 20 / 16,
              ),
            ),
            Text(
              rightStr ?? "",
              textAlign: TextAlign.right,
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                color: aceBlackColor,
                fontSize: getSp(16),
                fontWeight: rbood == true ? FontWeight.w700 : FontWeight.normal,
                height: 20 / 16,
              ),
            ),
          ],
        ),
      );
}
